from selenium import webdriver
from selenium.webdriver.support.select import Select
import time
import xlrd


# 登陆网站
def zhtj_login():
    drivers = webdriver.Chrome()
    drivers.get('https://zhtj.youth.cn/zhtj/')
    user_id = '410527198004140036'
    user_pw = '227718Lk'
    drivers.find_element_by_id('loginId').send_keys(user_id)
    drivers.find_element_by_id('password').send_keys(user_pw)
    user_yzm = input('Please input your yanzhengma:')
    drivers.find_element_by_id('yzm').send_keys(user_yzm)
    drivers.find_element_by_id('subBut').click()
    return drivers


# 进入添加成员界面
def enter_append_gui(drivers):
    # textContent是元素显示的内容如<p>haha</p> ,那么textContent的属性就是haha
    # print(drivers.find_element_by_id('side-menu').find_elements_by_tag_name('li')[20].get_attribute('textContent'))
    # print(drivers.find_element_by_id('side-menu').find_elements_by_tag_name('li')[20].text)
    # print(drivers.find_element_by_id('side-menu').get_attribute('textContent'))
    # 选择团员管理菜单
    side_menu_mem = drivers.find_element_by_id('side-menu').find_elements_by_tag_name('li')[20]
    side_menu_mem.click()
    time.sleep(2)
    side_menu_mem.find_elements_by_tag_name('li')[1].click()




# 选择添加团支部
def selected_class(drivers, classes):
    # 这个写法不太好，会把程序写死，不能进行自动选择
    # drivers.find_element_by_xpath("/html/body/div/div/div[2]/div[2]/div/div[2]/div[1]/div[1]/ul/li[2]/div/ul[1]/li[1]/a/span/span[2]").click()
    # time.sleep(5)
    # drivers.find_element_by_css_selector("#orgtreeDiv > div.el-tree.ztree.ztree.el-tree--highlight-current > ul > li:nth-child(2) > div > ul:nth-child(2) > li.el-tree-node__content > a > span > span.node_name").click()
    # 这个方法好可以让程序根据实际情况自己选择
    drivers.find_element_by_css_selector("a[title=%s" % (classes)).click()
    return drivers


# 读取个人信息
def get_mem_info(drivers):
    db = xlrd.open_workbook("/home/m/下载/2021年上半年推荐入团积极分子名单(1).xls")
    # 选中数据表
    table_info = db.sheet_by_name('member_info')
    print(table_info.name)
    # 逐行读取团员信息
    for i in range(1, table_info.nrows):
        print(table_info.row_values(i))
        mem_info_list = table_info.row_values(i)
        # 团员编号
        ty_bh = mem_info_list[0]
        ty_bj = mem_info_list[1]
        ty_name = mem_info_list[2]
        ty_sfzh = mem_info_list[3]
        ty_addr = mem_info_list[4]
        ty_tel = mem_info_list[5]
        dirver = selected_class(driver, ty_bj)
        time.sleep(2)
        drivers.find_element_by_id("userCode").send_keys(ty_bh)
        drivers.find_element_by_id("name").send_keys(ty_name)
        drivers.find_element_by_id("identityCardNo").send_keys(ty_sfzh)
        drivers.find_element_by_id("reidentityCardNo").send_keys(ty_sfzh)
        drivers.find_element_by_id("address").send_keys(ty_addr)
        drivers.find_element_by_id("mobile").send_keys(ty_tel)
        time.sleep(1)
        # 入团时间
        drivers.find_element_by_id("joinLeagueTime").send_keys("2020-05")
        """下拉式菜单"""
        # 民族
        drivers.find_element_by_id("nationalId").click()
        time.sleep(0.5)
        drivers.find_element_by_xpath("/html/body/div[3]/div[1]/div[1]/ul/li[1]").click()
        # 学历
        drivers.find_element_by_id("degreeId").click()
        time.sleep(0.5)
        drivers.find_element_by_xpath("/html/body/div[4]/div[1]/div[1]/ul/li[7]").click()
        time.sleep(1)
        # 职业
        drivers.find_element_by_id("jobType").click()
        time.sleep(1)
        drivers.find_element_by_xpath("/html/body/div[5]/div[1]/div[1]/ul/li[1]/span").click()
        # 具体职业
        drivers.find_element_by_id("job").click()
        time.sleep(1)
        drivers.find_element_by_xpath("/html/body/div[6]/div[1]/div[1]/ul/li[5]").click()
        # 政治面貌
        drivers.find_element_by_id("politicalId").click()
        time.sleep(1)
        drivers.find_element_by_xpath("/html/body/div[7]/div[1]/div[1]/ul/li[1]").click()
        # 是否团干部
        drivers.find_element_by_id("isCadres").click()
        time.sleep(1)
        drivers.find_element_by_xpath("/html/body/div[8]/div[1]/div[1]/ul/li[2]").click()
        # 是否志愿者
        drivers.find_element_by_id("isJoinVolunteerTime").click()
        time.sleep(1)
        drivers.find_element_by_xpath("/html/body/div[9]/div[1]/div[1]/ul/li[2]").click()
        # 是否递交入党申请
        drivers.find_element_by_id("isPartyRecord").click()
        time.sleep(1)
        drivers.find_element_by_xpath("/html/body/div[10]/div[1]/div[1]/ul/li[2]").click()
        # 上传文件
        drivers.find_element_by_id('upfileLeague').send_keys('/home/m/桌面/desktop/Ubuntu使用手册-中文.pdf')
        time.sleep(5)
        break
        drivers.find_element_by_xpath(
            "/html/body/div[1]/div/div[2]/div[2]/div/div[2]/div[2]/div/form/div/button[2]/span").click()
        time.sleep(5)


# 添加个人信息并提交
def append_info(drivers):
    pass


if __name__ == '__main__':
    driver = zhtj_login()
    time.sleep(3)
    dirver = enter_append_gui(driver)
    time.sleep(3)
    mem_info = get_mem_info(dirver)
    # classes = '河南省安阳市内黄县第二高级中学2020级学生团支部'
